package org.study.neo4j.boot.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.study.neo4j.boot.dto.StudentDTO;
import org.study.neo4j.boot.model.Student;
import org.study.neo4j.boot.service.StudentService;
import org.study.neo4j.boot.util.ConvertToDtoUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月09日
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping("/api/students")
public class StudentController {
    private final StudentService studentService;

    @GetMapping
    public List<StudentDTO> getAllStudents() {
        return studentService.getAllStudents().stream()
                .map(ConvertToDtoUtils::convertToStudentDTO)
                .collect(Collectors.toList());
    }

    @GetMapping("/{id}")
    public StudentDTO getStudentById(@PathVariable("id") Long id) {
        return ConvertToDtoUtils.convertToStudentDTO(studentService.getStudentById(id));
    }

    @PostMapping
    public Student createStudent(@Validated @RequestBody Student student) {
        return studentService.saveStudent(student);
    }

    @GetMapping("/by-course/{courseCode}")
    public List<StudentDTO> getStudentsByCourse(@PathVariable("courseCode") String courseCode) {
        return studentService.getStudentsByCourseCode(courseCode).stream()
                .map(ConvertToDtoUtils::convertToStudentDTO)
                .collect(Collectors.toList());
    }

    @GetMapping("/by-teacher/{teacherName}")
    public List<StudentDTO> getStudentsByTeacher(@PathVariable("teacherName") String teacherName) {
        return studentService.getStudentsByTeacherName(teacherName).stream()
                .map(ConvertToDtoUtils::convertToStudentDTO)
                .collect(Collectors.toList());
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable("id") Long id) {
        studentService.deleteStudent(id);
    }
}
